
import {
    Controller, Get, Req, Request, Query, Headers, Session, Ip, Param, Post, Body, Response, Next,
    Redirect, HttpCode, Header
} from '@nestjs/common'
import { User } from './user.decorator'

@Controller('users')
export class UserController {
   @Get('req')
   handleRequest(@Req() req, age, @Request() request){
        console.log(req.url)
        console.log(req.path)
        console.log(request.method)
        console.log(age)
        return 'handleRequestResult' 
   }
   @Get('query')
   handleQuery(@Query() query, @Query('id') id){
        console.log('query', query)
        console.log('id', id)
        return `query id: ${id}`
   }
   @Get('headers')
   handleHeaders(@Headers() Headers, @Headers('accept') accept) {
    console.log('Headers', Headers)
    return `accept:${accept}`
   }

   @Get('session')
   handleSession(@Session() session, @Session('pageView') pageView) {
    console.log('session', session)
    console.log('pageView', pageView)
    if (session.pageView) {
        session.pageView++
    } else {
        session.pageView = 1
    }
    return `pageView:${session.pageView}`
   }

   @Get('ip')
   handleIp(@Ip() ip) {
    console.log('ip', ip)
    return `ip:${ip}`
   }

   @Get(':username/info/:age')
   getUserNameInfo(@Param() params, @Param('username') username, @Param('age') age) {
    console.log('Param', params)
    console.log('username', username)
    console.log('age', age)
    return `age:${age}`
   }

   @Get('star/ab*de')
   handleWildcard() {
    return `handleWildcard`
   }

   @Post('create')
   @HttpCode(200)
   @Header('key2', 'value2')
   @Header('key1', 'value1')
   createUser(@Body() body, @Body('username') username){
    console.log('body',body)
    console.log('username',username)
    return 'create user'
   }

   @Get('response')
   response(@Response() response){
    response.send('wqewqe')
    return 'response'
   }

   @Get('passthrough')
   passthrough(@Response({passthrough: true}) response){
    response.setHeader('key', 'value')
    return 'response'
   }

   @Get('next')
   next(@Next() next){
    console.log('next', next)
    next()
   }
   @Get('/redirect')
   @Redirect('/users/req', 301)
   handleRedirect(){

   }
   @Get('/redirect2')
   handleRedirect2(@Query('version') version){
    return {url: `http://baidu.com/s?wd=${version}`}
   }

   @Get('/custom')
   customParamDecorator(@User('role') role, @User() user){
    console.log('role', role)
    return user
   }



}